﻿using System;
using System.Collections.Generic;

using System.Text;

namespace System.Qizx.Xml
{
    /// <summary>
    /// Resolves the URI of external resources in transformations.
    /// </summary>
    public class DefaultTransformerResolver : System.Xml.XmlUrlResolver
    {
        private TransformerResolver uriResolver;

        public TransformerResolver UriResolver
        {
            get
            {
                return this.uriResolver;
            }
            set
            {
                this.uriResolver = value;
            }
        }

        private BasicSource style;
        public BasicSource Style
        {
            get
            {
                return this.style;
            }
            set
            {
                this.style = value;
            }
        }


        /// <summary>
        /// Resolves URI of parent and calls user implementation method.
        /// </summary>
        public override System.Uri ResolveUri(System.Uri BaseUri, string relativeUri)
        {
            string baseUri = null;

            if (BaseUri != null)
                baseUri = BaseUri.AbsolutePath;
            else
                baseUri = this.style.Id;

            if (this.uriResolver != null)
            {
                BasicSource resolvedSource = this.uriResolver.ResolveURI(relativeUri, baseUri);
                System.Uri resolvedUri = null;

                if (BaseUri != null)
                    resolvedUri = new System.Uri(BaseUri, resolvedSource.Id);
                else
                    resolvedUri = new System.Xml.XmlUrlResolver().ResolveUri(null, resolvedSource.Id);

                return resolvedUri;
            }

            else
                return new System.Xml.XmlUrlResolver().ResolveUri(BaseUri, relativeUri);
        }
    }
}
